﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;
using System.Linq;
namespace Billing.Transactions
{
    public partial class AccountCorrectionForm : Billing.Transactions.TransactionBase
    {
        public AccountCorrectionForm()
        {
            InitializeComponent();
        }

        AccountsView ClientFrom = null;
        AccountsView ClientTo = null;

        bool clientTypesAreDifferent;

        private void ShowHideDestinationAmount()
        {
            if (ClientFrom == null || ClientTo == null)
                clientTypesAreDifferent = false;
            else clientTypesAreDifferent = ClientFrom.Client.ClientTypeID != ClientTo.Client.ClientTypeID;

            txtDstAmount.Enabled = clientTypesAreDifferent;
        }

        private void trvClientFrom_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.AccountsSelectionForm frm = new Billing.Win.AccountsSelectionForm(false);

            frm.AllowSelect = true;

            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClientFrom = (AccountsView)frm.SelectedObjects[0].CurrentObject;
                this.trvClientFrom.BaseObject = ClientFrom;
                ShowHideDestinationAmount();
            }
        }

        private void trvClientTo_ChooseBootonClick(object sender, Infragistics.Win.UltraWinEditors.EditorButtonEventArgs e)
        {
            Billing.Win.AccountsSelectionForm frm = new Billing.Win.AccountsSelectionForm(false);
            frm.AllowSelect = true;
            if (ClientFrom != null && ClientFrom.Client.ClientTypeID==1)
            {
                frm.ClientsSearch.ClientID = this.BillingDataContext.GetAccountNumberLastClientID(ClientFrom.AccountNumberID);
                frm.ClientsSearch.Search();
            }
            if (frm.ShowDialog() == DialogResult.OK)
            {
                ClientTo = (AccountsView)frm.SelectedObjects[0].CurrentObject;
                this.trvClientTo.BaseObject = ClientTo;
                
                ShowHideDestinationAmount();
                txtSrcAmount_ValueChanged(sender, EventArgs.Empty);
            }
        }

        public override void OnSave()
        {
            try
            {
                decimal srcAmount, dstAmount;
                srcAmount = dstAmount = Convert.ToDecimal(this.txtDstAmount.Value);
                if (srcAmount == 0)
                {
                    this.ShowWarning("ნულოვან თანხაზე გატარება დაუშვებელია!");
                    return;
                }

                if (this.ClientFrom.ID == this.ClientTo.ID)
                {
                    this.ShowWarning("ერთი და იგივე ანგარიშზე თანხის გადატანა დაუშვებელია!");
                    return;
                }
                this.CurrentHeader.HeaderTypeID = 3;
                this.CurrentHeader.Description = "Accounts Correction Transfer";

                this.AddNewLine();
                this.CurrentLine.AccountID = ClientFrom.ID;
                this.CurrentLine.ItemID = 10;
                this.CurrentLine.TaxID = ClientFrom.Client.TaxID;
                this.CurrentLine.Amount = srcAmount;

                this.AddNewLine();
                this.CurrentLine.AccountID = ClientTo.ID;
                this.CurrentLine.ItemID = 10;
                this.CurrentLine.TaxID = ClientTo.Client.TaxID;
                this.CurrentLine.Amount = -dstAmount;

                base.OnSave();
            }
            catch (Exception ex)
            {
                this.ShowError(ex);
            }
            this.Close();
        }

        private void txtSrcAmount_ValueChanged(object sender, EventArgs e)
        {
            if (!clientTypesAreDifferent)
            {
                txtDstAmount.Value = txtSrcAmount.Value;
            }
        }

        private void txtSrcAmount_Enter(object sender, EventArgs e)
        {
            this.txtSrcAmount.SelectAll();
        }

        private void txtDstAmount_Enter(object sender, EventArgs e)
        {
            this.txtDstAmount.SelectAll();
        }
    }
}
